﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class julia : MonoBehaviour
{

    // Use this for initialization
    void Start()
    {
        //先准备好一些颜色
    }

    // Update is called once per frame
    void Update()
    {

    }
    static Color32[] genColor()
    {
        var colors = new Color32[256];
        for (int i = 0; i < 256; i++)
        {
            byte B = (byte)(i * 5 % 256);
            byte G = (byte)((i + 1) * 5 % 256);
            byte R = (byte)((i + 2) * 5 % 256);
            colors[i] = new Color32(R, G, B, 255);//使用了库函数EGERGB 
        }
        return colors;
    }
    static double delay;

    void OnGUI()
    {
        if (GUILayout.Button("do Julia"))
        {

            int width = 4096;
            int height = 4096;
            var colors = genColor();
            Color32[] data = new Color32[width * height];

            System.DateTime t0 = System.DateTime.Now;

            DoJulia(colors, data, width, height);
            System.DateTime t1 = System.DateTime.Now;
            delay = (t1 - t0).TotalSeconds;


            Texture2D tex = new Texture2D(width, height);
            tex.SetPixels32(data);
            tex.Apply();
            this.GetComponent<MeshRenderer>().material.mainTexture = tex;


        }
        if(GUILayout.Button("do julia cpp"))
        {

            int width = 4096;
            int height = 4096;
            var colors = genColor();
            Color32[] data = new Color32[width * height];

            System.DateTime t0 = System.DateTime.Now;
            juliaplugin.DoJulia(colors, data, width, height);
            System.DateTime t1 = System.DateTime.Now;
            delay = (t1 - t0).TotalSeconds;


            Texture2D tex = new Texture2D(width, height);
            tex.SetPixels32(data);
            tex.Apply();
            this.GetComponent<MeshRenderer>().material.mainTexture = tex;

        }
        GUILayout.Label("耗时 （秒）=" + delay);
    }

    struct Complex
    {
        public double r;
        public double i;
    }
;

    static void DoJulia(Color32[] colors, Color32[] buf, int width, int height)
    {
        const double x_min = -1.5, y_min = -1.5;
        const double x_max = 1.5, y_max = 1.5;

        double dx, dy = 0;
        dx = (x_max - x_min) / (width - 1);
        dy = (y_max - y_min) / (height - 1);
        for (int nx = 0; nx < width; nx++)
        {
            for (int ny = 0; ny < height; ny++)
            {
                int k = 0;
                Complex x0;
                x0.r = x_min + nx * dx;
                x0.i = y_min + ny * dy;
                iteration(colors, buf, width, x0, k, nx, ny);
            }
        }
    }
    static void iteration(Color32[] colors, Color32[] buf, int width, Complex a, int k, int nx, int ny)
    {
        const int K = 255;
        const int M = 100;
        const double p = 0.33, q = 0.4;//常数C 
        double tmp = a.r * a.r + a.i * a.i;
        if (tmp > M)
        {
            buf[ny * width + nx] = colors[k];
            return;
        }
        if (k == K)
        {
            buf[ny * width + nx] = colors[0];
            return;
        }
        if (tmp <= M && k < K)
        {
            Complex b;
            b.r = a.r * a.r - a.i * a.i + p;
            b.i = 2 * a.r * a.i + q;
            k++;
            iteration(colors, buf, width, b, k, nx, ny);
        }
    }

}
